//
//  FittingViewController.m
//  Menu
//
//  Created by YouLu on 15/7/3.
//  Copyright (c) 2015年 lijiangang. All rights reserved.
//

#import "FittingViewController.h"

@interface FittingViewController ()

@end

@implementation FittingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fittingModelArray = [NSMutableArray array];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = CGRectMake(0, 64+260*HEIGHT+40*HEIGHT, MyWidth, MyHeight-64-49-260*HEIGHT-40*HEIGHT);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MyHeight, MyHeight-64-49-260*HEIGHT-40*HEIGHT) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView.bounces = YES;
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.HUD.labelText = @"loading";
    [self.HUD show:YES];
}
-(void)loadData:(NSString *)vegetableId
{
    
    NSString *stepStrUrl = [NSString stringWithFormat:@"http://121.41.84.91:80/HandheldKitchen/api/vegetable/tblvegetable!getIntelligentChoice.do?vegetable_id=%@&type=3&phonetype=0&is_traditional=0",vegetableId];
    
    [AFNTool get:stepStrUrl Body:nil HttpResult:JSON success:^(id result) {
        
        NSMutableArray *dataArray = [result objectForKey:@"data"];
        NSMutableDictionary *beanDic = [result objectForKey:@"bean"];
        NSInteger arrayCount = [[beanDic objectForKey:@"totalRecord"] integerValue];
        if (arrayCount == 2) {
            NSMutableDictionary *oneDataDic = [dataArray objectAtIndex:0];
            NSMutableArray *fittingArray = [oneDataDic objectForKey:@"Fitting"];
            FittingModel *fittingModel = [[FittingModel alloc] initWithDic:oneDataDic];
            [self.fittingModelArray addObject:fittingModel];
            for (NSMutableDictionary *dic in fittingArray) {
                self.number++;
                FittingModel *fittingModel = [[FittingModel alloc] initWithDic:dic];
                [self.fittingModelArray addObject:fittingModel];
                
            }
            NSMutableDictionary *twoDataDic = [dataArray objectAtIndex:1];
            NSMutableArray *gramArray = [twoDataDic objectForKey:@"Gram"];
            FittingModel *gramModel = [[FittingModel alloc] initWithDic:twoDataDic];
            [self.fittingModelArray addObject:gramModel];
            for (NSMutableDictionary *dic in gramArray) {
                FittingModel *fittingModel = [[FittingModel alloc] initWithDic:dic];
                [self.fittingModelArray addObject:fittingModel];
                
            }
            
        }
        if (arrayCount == 1) {
            
            NSMutableDictionary *oneDataDic = [dataArray objectAtIndex:0];
            NSMutableArray *fittingArray = [oneDataDic objectForKey:@"Fitting"];
            FittingModel *fittingModel = [[FittingModel alloc] initWithDic:oneDataDic];
            [self.fittingModelArray addObject:fittingModel];
            for (NSMutableDictionary *dic in fittingArray) {
                self.number++;
                FittingModel *fittingModel = [[FittingModel alloc] initWithDic:dic];
                [self.fittingModelArray addObject:fittingModel];
            }
        }
        [self.tableView reloadData];
        [self.HUD hide:YES];
    } failure:^(NSError *error) {
        
        self.HUD.labelText = @"加载失败";
        [self.HUD hide:YES afterDelay:1];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.fittingModelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        static NSString *cellIdentifier = @"fittingCell";
        FittingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[FittingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        
        UILabel *fittingLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, 0, 44, 44)];
        fittingLabel.layer.cornerRadius = 5;
        fittingLabel.layer.masksToBounds = YES;
        fittingLabel.text = @"相宜";
        fittingLabel.font = [UIFont systemFontOfSize:22];
        fittingLabel.textAlignment = NSTextAlignmentCenter;
        fittingLabel.backgroundColor = [UIColor greenColor];
        FittingModel *model = [self.fittingModelArray objectAtIndex:0];
        NSString *str = [NSString stringWithFormat:@"%@与下列实物相宜",model.materialName];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80*WIDTH, 0, 200*WIDTH, 44)];
        if (str.length*17*WIDTH > 200*WIDTH) {
            label.font = [UIFont systemFontOfSize:14*WIDTH];
        }
        label.textColor = [UIColor greenColor];
        label.text = str;
        UIImageView *picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(290*WIDTH, 0, 44, 44)];
        [picImageView sd_setImageWithURL:[NSURL URLWithString:model.imageName]];
        [cell addSubview:picImageView];
        [cell addSubview:fittingLabel];
        [cell addSubview:label];
        [fittingLabel release];
        [label release];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == self.number+1) {
        static NSString *cellIdentifier = @"gramCell";
        FittingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[FittingTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        UILabel *gramLabel = [[UILabel alloc] initWithFrame:CGRectMake(20*WIDTH, 0, 44, 44)];
        gramLabel.layer.cornerRadius = 5;
        gramLabel.layer.masksToBounds = YES;
        gramLabel.text = @"相克";
        gramLabel.font = [UIFont systemFontOfSize:22];
        gramLabel.textAlignment = NSTextAlignmentCenter;
        gramLabel.backgroundColor = [UIColor redColor];
        FittingModel *model = [self.fittingModelArray objectAtIndex:0];
        NSString *str = [NSString stringWithFormat:@"%@与下列实物相克",model.materialName];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(80*WIDTH, 0, 200*WIDTH, 44)];
        if (str.length*17*WIDTH > 200*WIDTH) {
            label.font = [UIFont systemFontOfSize:14*WIDTH];
        }
        label.textColor = [UIColor redColor];
        label.text = str;
        UIImageView *picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(290*WIDTH, 0, 44, 44)];
        [picImageView sd_setImageWithURL:[NSURL URLWithString:model.imageName]];
        [cell addSubview:picImageView];
        [cell addSubview:gramLabel];
        [cell addSubview:label];
        [gramLabel release];
        [label release];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        static NSString *cellIdentifier = @"cell";
        GramTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (cell == nil) {
            cell = [[GramTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.fittingModel = [self.fittingModelArray objectAtIndex:indexPath.row];
        
        return cell;
    }
    
   
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
